package com.zxf.appliction.util;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.zxf.appliction.domain.Article;

 
 
/**
 * 
 * @author jemond
 *
 * 2015年6月26日上午9:42:48
 */
public  class ObjectExcelView extends AbstractExcelView{
 
	public HSSFWorkbook exportExcel(List<Article> articles){  
        //创建一个新的excel  
        HSSFWorkbook workbook = new HSSFWorkbook();  
        //创建一个sheet  
        HSSFSheet sheet = workbook.createSheet();  
          
        //设置每列的宽度  
        sheet.setColumnWidth(0, 4000);  
        sheet.setColumnWidth(1, 4000);  
        sheet.setColumnWidth(2, 4000);  
        sheet.setColumnWidth(3, 4000);  
        sheet.setColumnWidth(4, 5000);  
        sheet.setColumnWidth(5, 5000);  
        sheet.setColumnWidth(6, 5000);  
        sheet.setColumnWidth(7, 5000);  
          
        //设置字体  
        HSSFFont font = workbook.createFont();  
        font.setFontName("Verdana");  
        font.setBoldweight((short) 100);  
        font.setFontHeight((short) 300);  
        font.setColor(HSSFColor.BLUE.index);  
          
        //设置headStyle样式  
        HSSFCellStyle headStyle = workbook.createCellStyle();  
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
        headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
        headStyle.setFont(font);  
          
        //普通cellStyle样式  
        HSSFCellStyle cellStyle = workbook.createCellStyle();  
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
        HSSFCellStyle cellStyle2 = workbook.createCellStyle(); 
        HSSFDataFormat format= workbook.createDataFormat();
        cellStyle2.setDataFormat(format.getFormat("yyyy年m月d日"));
          
        //设置sheet名称  
        workbook.setSheetName(0, "通讯录");  
          
        HSSFRow row = sheet.createRow(0);  
          
        //设置行高  
        row.setHeight((short)500);  
        //row.setRowStyle(style);  
        HSSFCell cell0 = row.createCell(0);  
        HSSFCell cell1 = row.createCell(1);  
        HSSFCell cell2 = row.createCell(2);  
        HSSFCell cell3 = row.createCell(3);  
        HSSFCell cell4 = row.createCell(4); 
        HSSFCell cell5 = row.createCell(5);  
        HSSFCell cell6 = row.createCell(6);  
        HSSFCell cell7 = row.createCell(7);  
          
        //设置字符集  
        cell0.setCellType(HSSFCell.ENCODING_UTF_16);  
        cell1.setCellType(HSSFCell.ENCODING_UTF_16);  
        cell2.setCellType(HSSFCell.ENCODING_UTF_16);  
        cell3.setCellType(HSSFCell.ENCODING_UTF_16);  
        cell4.setCellType(HSSFCell.ENCODING_UTF_16); 
        cell5.setCellType(HSSFCell.ENCODING_UTF_16);  
        cell6.setCellType(HSSFCell.ENCODING_UTF_16);  
        cell7.setCellType(HSSFCell.ENCODING_UTF_16); 
          
        //设置cell样式  
        cell0.setCellStyle(headStyle);  
        cell1.setCellStyle(headStyle);  
        cell2.setCellStyle(headStyle);  
        cell3.setCellStyle(headStyle);  
        cell4.setCellStyle(headStyle); 
        cell5.setCellStyle(headStyle);  
        cell6.setCellStyle(headStyle);  
        cell7.setCellStyle(headStyle);
          
        //设置内容  
        cell0.setCellValue("ID号");  
        cell1.setCellValue("作者");  
        cell2.setCellValue("文章类型");  
        cell3.setCellValue("添加时间");  
        cell4.setCellValue("来源"); 
        cell5.setCellValue("标题"); 
        cell6.setCellValue("时间"); 
        cell7.setCellValue("概要"); 
          
        for (int i = 0; i < articles.size(); i++) {  
            row = sheet.createRow(i+1);  
            row.setHeight((short)350);  
            cell0 = row.createCell(0);  
            cell1 = row.createCell(1);  
            cell2 = row.createCell(2);  
            cell3 = row.createCell(3);  
            cell4 = row.createCell(4);  
            cell5 = row.createCell(5);  
            cell6 = row.createCell(6);  
            cell7 = row.createCell(7);  
              
            //设置cell样式  
            cell0.setCellStyle(cellStyle);  
            cell1.setCellStyle(cellStyle);  
            cell2.setCellStyle(cellStyle);  
            cell3.setCellStyle(cellStyle);  
            cell4.setCellStyle(cellStyle); 
            cell5.setCellStyle(cellStyle);  
            cell6.setCellStyle(cellStyle2);  
            cell7.setCellStyle(cellStyle); 
              
            //设置字符集  
            cell0.setCellType(HSSFCell.CELL_TYPE_NUMERIC);  
            cell1.setCellType(HSSFCell.ENCODING_UTF_16);  
            cell2.setCellType(HSSFCell.CELL_TYPE_NUMERIC);  
            cell3.setCellType(HSSFCell.ENCODING_UTF_16);  
            cell4.setCellType(HSSFCell.ENCODING_UTF_16); 
            cell5.setCellType(HSSFCell.ENCODING_UTF_16);  
            cell6.setCellType(HSSFCell.ENCODING_UTF_16);  
            cell7.setCellType(HSSFCell.ENCODING_UTF_16);
              
            //设置内容  
            cell0.setCellValue(articles.get(i).getArticleid());  
            cell1.setCellValue(articles.get(i).getAuthor());
            if(articles.get(i).getChannelid()==1){
            	cell2.setCellValue("军事"); 
            }
            if(articles.get(i).getChannelid()==2){
            	cell2.setCellValue("财经"); 
            }
            if(articles.get(i).getChannelid()==3){
            	cell2.setCellValue("社会"); 
            }
            if(articles.get(i).getChannelid()==4){
            	cell2.setCellValue("历史"); 
            }
            if(articles.get(i).getChannelid()==5){
            	cell2.setCellValue("文化"); 
            }
             
            cell3.setCellValue(articles.get(i).getAddeddate());  
            cell4.setCellValue(articles.get(i).getSource());
            cell5.setCellValue(articles.get(i).getTitle());  
            cell6.setCellValue(articles.get(i).getPublishdate());  
            cell7.setCellValue(articles.get(i).getSummary());
        }  
        return workbook;  
    }

	@Override
	protected void buildExcelDocument(Map<String, Object> arg0,
			HSSFWorkbook arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
 
 
     
}
 
